'''oto/util.py

This module contains utility methods for Oranges to Oranges.
'''

__author__ = 'Ryan Rousseau <ryan.rousseau@gmail.com>'
__license__ = '''Oranges to Oranges
Copyright (C) 2006  Ryan Rousseau

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
'''

# system imports #

import gconf
import glob
import os
import shelve
import sys
import tarfile
import time

# amenity imports #

from constants import *

__all__ = ['set_oto_dir', 'get_oto_dir', 'set_log_dir', 'get_log_dir',
           'set_site_dir', 'get_site_dir', 'set_deck_dir', 'get_deck_dir',
           'get_site_names', 'delete_site', 'get_deck_names', 'delete_deck',
           'get_logfile_names']

# get gconf client #
gconf_client = gconf.client_get_default()

# set_oto_dir #

def set_oto_dir(oto_dir):
    '''set_oto_dir(oto_dir)

Set the root directory for oto to 'oto_dir'.
'''
    gconf_client.set_string(GCONF_OTO_DIR, oto_dir)
    if not os.path.exists(oto_dir):
        os.makedirs(oto_dir)

# get_oto_dir #

def get_oto_dir():
    '''get_oto_dir()

Return the root directory for oto.
'''
    return gconf_client.get_string(GCONF_OTO_DIR)


# set_log_dir #

def set_log_dir(log_dir):
    '''set_log_dir(log_dir)

Set the log directory for oto to 'log_dir'.
'''
    gconf_client.set_string(GCONF_LOG_DIR, log_dir)
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)


# get_log_dir #

def get_log_dir():
    '''get_log_dir()

Return the log directory for oto.
'''
    return gconf_client.get_string(GCONF_LOG_DIR)


# set_site_dir #

def set_site_dir(site_dir):
    '''set_site_dir(site_dir)

Set the site directory for oto to 'site_dir'.
'''
    gconf_client.set_string(GCONF_SITE_DIR, site_dir)
    if not os.path.exists(site_dir):
        os.makedirs(site_dir)


# get_site_dir #

def get_site_dir():
    '''get_site_dir()

Return the site directory for oto.
'''
    return gconf_client.get_string(GCONF_SITE_DIR)

# set_deck_dir #

def set_deck_dir(deck_dir):
    '''set_deck_dir(deck_dir)

Set the deck directory for oto to 'deck_dir'.
'''
    gconf_client.set_string(GCONF_DECK_DIR, deck_dir)
    if not os.path.exists(deck_dir):
        os.makedirs(deck_dir)

# get_deck_dir #

def get_deck_dir():
    '''get_deck_dir()

Return the deck directory for oto.
'''
    return gconf_client.get_string(GCONF_DECK_DIR)

# get_site_names #

def get_site_names():
    '''get_site_names()

Return the list of sites.
'''
    sites = []
    for site in glob.glob(os.path.join(get_site_dir(), '*.site')):
        site = os.path.split(site)[1]
        sites.append(site.rsplit('.', 1)[0])
    return sites

# delete_site #

def delete_site(name):
    '''delete_site(name)

Remove the site identified by 'name'
'''

    filename = os.path.join(get_site_dir(), '%s.site' % name)
    os.remove(filename)

# get_deck_names #

def get_deck_names():
    '''get_deck_names()

Return the list of decks.
'''
    decks = []
    for deck in glob.glob(os.path.join(get_deck_dir(), '*.deck')):
        deck = os.path.split(deck)[1]
        decks.append(deck.rsplit('.', 1)[0])
    return decks

# delete_deck #

def delete_deck(name):
    '''delete_deck(name)

Remove the deck identified by 'name'
'''

    filename = os.path.join(get_deck_dir(), '%s.deck' % name)
    os.remove(filename)

# get_logfile_names #

def get_logfile_names():
    '''get_logfile_names()

Returns the two names for the log files.
'''
    # get the important time parameters and format the names
    year, month, day, hour, min, sec = time.localtime()[:6]
    amenity_name = 'Oto %d-%02d-%02d %02d:%02d:%02d' % (year, month, day,
                                                            hour, min, sec)
    server_name = 'Server %d-%02d-%02d %02d:%02d:%02d' % (year, month, day,
                                                          hour, min, sec)
    amenity_name = os.path.join(get_log_dir(), amenity_name)
    server_name = os.path.join(get_log_dir(), server_name)
    return amenity_name, server_name


# check if the fields are set up in gconf

if not get_oto_dir():
    set_oto_dir(DEFAULT_OTO_DIR)

if not get_log_dir():
    set_log_dir(DEFAULT_LOG_DIR)

if not get_site_dir():
    set_site_dir(DEFAULT_SITE_DIR)
